package com.bilibili.service.impl;

import com.bilibili.entity.User;
import com.bilibili.entity.UserFollowing;
import com.bilibili.entity.UserFollowingGroup;
import com.bilibili.mapper.UserFollowingMapper;
import com.bilibili.service.FollowingService;
import com.bilibili.service.UserFollowingGroupService;
import com.bilibili.service.UserService;
import io.mybatis.service.AbstractService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Service
public class FollowingServiceImpl extends AbstractService<UserFollowing, Long, UserFollowingMapper> implements FollowingService {

    private final UserService userService;
    private final UserFollowingGroupService userFollowingGroupService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addFollow(Long userId, Long followingUserId, List<Long> groupIds) {
        User user = userService.findById(userId);
        if (Objects.isNull(user)) {
            throw new RuntimeException("用户不存在");
        }
        User followingUser = userService.findById(followingUserId);
        if (Objects.isNull(followingUser)) {
            throw new RuntimeException("关注用户不存在");
        }
        List<UserFollowingGroup> userFollowingGroups = userFollowingGroupService.queryListByIds(groupIds);
        if (CollectionUtils.isEmpty(userFollowingGroups) || Integer.compare(userFollowingGroups.size(), groupIds.size()) != 0) {
            throw new RuntimeException("存在非法分组");
        }
        List<UserFollowing> userFollowingList = this.baseMapper.wrapper().eq(UserFollowing::getUserId, userId).eq(UserFollowing::getFollowingUserId, followingUserId).list();
        if (CollectionUtils.isNotEmpty(userFollowingList)) {
            List<Long> existsGroupList = userFollowingList.stream().map(UserFollowing::getGroupId).collect(Collectors.toList());
            groupIds.removeAll(existsGroupList);
        }
        List<UserFollowing> userFollowings = new ArrayList<>();
        UserFollowing userFollowing = new UserFollowing();
        userFollowing.setUserId(userId);
        userFollowing.setFollowingUserId(followingUserId);
        userFollowing.setCreateTime(new Date());
        for (Long groupId : groupIds) {
            userFollowing.setGroupId(groupId);
            userFollowings.add(userFollowing);
        }
        // 批量添加用户关注关系
        this.baseMapper.batchInsert(userFollowings);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeFollow(Long userId, Long followingUserId) {
        this.baseMapper.wrapper().eq(UserFollowing::getUserId, userId).eq(UserFollowing::getFollowingUserId, followingUserId).delete();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateFollow(Long userId, Long followingUserId, List<Long> groupIds) {

    }
}
